package com.luck;

import com.alibaba.fastjson.JSON;
import com.luck.dao.UserClassMapper;
import com.luck.model.dto.BlogInfoDto;
import com.luck.model.po.UserClass;
import com.luck.model.pojo.AssignWeight;
import org.apache.commons.io.IOUtils;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Collection;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Map;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/httpController")
public class HttpController {

    private Logger logger = Logger.getLogger(HttpController.class);

    @Resource
    private UserClassMapper userClassMapper;
    /**
     * GET
     * @param value
     * @return
     */
    @GetMapping("/doGet")
    @ResponseBody
    public String doGet(@RequestParam("value") String value) {
        logger.info("http:success doGet>>." + value);
        return "请求以收到 doGet().";
    }

    /**
     * POST : JSON
     * json字符串
     * @param blogInfoDto
     * @return
     */
    @PostMapping(value = "/doPostJson", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String doPostJson(@RequestBody BlogInfoDto blogInfoDto) {
        logger.info("http:success doPost>>type:json,request:" + blogInfoDto);
        UserClass userClass = new UserClass();
        userClass.setClassName(blogInfoDto.getUserName());
        userClassMapper.insertSelective(userClass);
        return "请求以收到 doPostJson.";
    }

    /**
     * POST : FORM
     * 表单提交
     * @param blogInfoDto
     * @return
     */
    @PostMapping(value = "/doPostForm", produces = "application/x-www-form-urlencoded")
    public String doPostForm(BlogInfoDto blogInfoDto) {
        logger.info("http:success doPost>>type:Form,request:" + blogInfoDto);
        return "请求以收到 doPostForm.";
    }

    /**
     * POST : FORM-DATA
     * 表单提交 + 上传文件
     * @param blogInfoDto
     * @param file
     * @return
     */
    @PostMapping(value = "/doPostFormData", produces = "multipart/form-data;charset=utf-8")
    public String doPostFormData(BlogInfoDto blogInfoDto, @RequestParam("imgFile") MultipartFile file) {
        logger.info("http:success doPost>>type:FormData,request:" + blogInfoDto + ",文件名:" + file.getName());
        return "请求以收到 doPostFormData.";
    }

    /**
     * POST : TEXT
     * 纯文本
     * @param values
     * @return
     */
    @PostMapping(value = "/doPostText", produces = "text/plain")
    public String doPostText(@RequestBody String values) {
        logger.info("http:success doPost>>type:Text,request:" + values);
        return "请求以收到 doPostText.";
    }

    /**
     * POST : TEXT
     * xml
     * @param values
     * @return
     */
    @PostMapping(value = "/doPostXml")
    public String doPostXml(@RequestBody String values) {
        logger.info("http:success doPost>>type:Xml,request:" + values);
        return "请求以收到 doPostXml.";
    }

    @RequestMapping("/signOut")
    @ResponseBody
    public String doPay(HttpServletRequest request) throws Exception {
        String xmlString = IOUtils.toString(request.getReader());
        logger.info("来了老弟:--->" + xmlString);
        Thread.sleep(Long.parseLong(xmlString));
        logger.info("睡了秒钟:--->" + xmlString);
        return xmlString;
    }

    @RequestMapping(value = "/signOut2", method = RequestMethod.POST)
    public String xunLianReleaseBankCardNotice(@RequestBody String request) {
        return request;
    }

    @RequestMapping(value = "/test", method = RequestMethod.POST)
    public AssignWeight assignWeight(@RequestBody AssignWeight assignWeight, HttpServletResponse httpServletResponse, HttpServletRequest httpServletRequest) {
        Collection<String> headerNames = httpServletResponse.getHeaderNames();
        //for (int i = 0; i < headerNames.size(); i++) {
        String contentType = httpServletResponse.getContentType();
        System.out.println("contentType:" + contentType);
        for (String s:headerNames) {
            System.out.println("begin:"+httpServletResponse.getHeader(s));
        }
        Enumeration<String> headerNamesreq = httpServletRequest.getHeaderNames();
        System.out.println(httpServletRequest.getHeader(headerNamesreq.nextElement()));
        System.out.println("request json begin:" + JSON.toJSONString(httpServletRequest.getHeaderNames()));
        assignWeight.setLevel(1);
        assignWeight.setCount(2);
        assignWeight.setName("test");
        assignWeight.setWeight(3);
        System.out.println(httpServletResponse);
        Collection<String> headerNames2 = httpServletResponse.getHeaderNames();
        //for (int i = 0; i < headerNames.size(); i++) {
        for (String s:headerNames2) {
            System.out.println("end:"+httpServletResponse.getHeader(s));
        }
        String contentType2 = httpServletResponse.getContentType();
        System.out.println("contentType2:" + contentType2);

        Enumeration<String> headerNamesreq2 = httpServletRequest.getHeaderNames();
        System.out.println(httpServletRequest.getHeader(headerNamesreq2.nextElement()));
        System.out.println("request json begin:" + JSON.toJSONString(httpServletRequest.getHeaderNames()));
        return new AssignWeight();
    }
}
